package com.example.mybatisplus.lock.service;

import com.example.mybatisplus.lock.domain.User;
import com.example.mybatisplus.lock.mapper.LockMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 悲观锁和乐观锁的service层
 *
 * @author zhouxl
 * @DATE 2020/9/24
 * @descriptoin
 */
@Component
public class LockService {

    @Autowired
    private LockMapper lockMapper;

    /**
     * 悲观锁的实现过程
     *
     * @param map
     * @return
     */
    public Map<String, Object> bgLock(Map<String, Object> map) {
        Map<String, Object> result = new HashMap<>();
        String userid = map.get("userid").toString();
        User user = lockMapper.queryUserById(userid);
        if (Objects.isNull(user)) {
            result.put("code", "400");
            result.put("msg", "当前用户不存在");
            return result;
        }
        // 模拟网络延迟
        long time = Long.parseLong(map.get("time").toString());
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            result.put("code", "400");
            result.put("msg", "线程异常");
            return result;
        }
        // 修改用户信息
        int count = lockMapper.upateUser(map);
        if (count != 0) {
            result.put("code", "200");
            result.put("msg", "修改成功");
        } else {
            result.put("code", "200");
            result.put("msg", "修改失败");
        }

        PageHelper.getOrderBy();

        return result;
    }

    /**
     * 乐观锁的简单实现
     *
     * @param map
     * @return
     */
    public Map<String, Object> lgLock(Map<String, Object> map) {

        Map<String, Object> result = new HashMap<>();
        String userid = map.get("userid").toString();
        User user = lockMapper.queryUserByIdBg(userid);
        if (Objects.isNull(user)) {
            result.put("code", "400");
            result.put("msg", "当前用户不存在");
            return result;
        }
        // 模拟网路延时
        long time = Long.parseLong(map.get("time").toString());
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            result.put("code", "400");
            result.put("msg", "线程异常");
            return result;
        }
        // 修改用户信息
        user.setName(map.get("name").toString());
        int count = lockMapper.upateUserLg(user);
        if (count != 0) {
            result.put("code", "200");
            result.put("msg", "修改成功");
        } else {
            result.put("code", "200");
            result.put("msg", "修改失败");
        }
        return result;

    }

    /**
     * 通过乐观锁解析数据数据信息
     *
     * @param map
     * @return
     */
    public Map<String, Object> lgLockAnaly(Map<String, Object> map) {

        Map<String, Object> result = new HashMap<>();
        /*
        1.查询数据
        2.延迟一段时间
        3.解析数据
         */
        map.put("min", 0);
        map.put("max", 10);
        List<User> userList = lockMapper.queryUsers(map);
        Object timeObj = map.get("time");
        long time = 0;
        if (Objects.isNull(timeObj)) {
            time = 50000;
        } else {
            time = Long.parseLong(map.get("time").toString());
        }
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            result.put("code", "400");
            result.put("msg", "线程异常");
            return result;
        }
        // 开始解析
        int total = 0;
        System.out.println(userList.get(0).getVersion());
        for (User user : userList) {
            user.setLc("2");
            int count = lockMapper.upateUserLg(user);
            total += count;
        }
        result.put("code", "200");
        result.put("msg", "解析数据总量:" + userList.size() + "," +
                "解析成功数据量:" + total + "," +
                "解析失败数据量:" + (userList.size() - total));
        System.out.println(result.get("msg"));
        return result;

    }

    public Object queryUserById() {
        String userid = "67af47cd233a46428c1a4dbd0dd604b7";
        return lockMapper.queryUserByIdLg(userid);
    }

    /**
     * 悲观锁的实现过程
     *
     * @param map
     * @return
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Object bgLockAnaly(Map<String, Object> map) {
        Map<String, Object> result = new HashMap<>();
        Object useridObj = map.get("userid");
        if (Objects.isNull(useridObj)) {
            result.put("code", "400");
            result.put("msg", "参数异常");
            return result;
        }
        User user = lockMapper.queryUserById(useridObj.toString());
        if (Objects.isNull(user)) {
            result.put("code", "400");
            result.put("msg", "对象编号[" + useridObj.toString() + "]所对应的对象信息不存在");
            return result;
        }
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            result.put("code","400");
//            result.put("msg","线程异常");
//            return result;
//        }
        int update = lockMapper.upateUserBg(user);
        if (update != 0) {
            result.put("code", "200");
            result.put("msg", "修改用户信息成功");
        } else {
            result.put("code", "400");
            result.put("msg", "修改用户信息失败");
        }
        System.out.println(user);
        System.out.println(result.get("msg"));
        return result;
    }
}
